#include "QEnrolDialog.h"

QEnrolDialog::QEnrolDialog(QWidget *parent)
    : QDialog(parent)
{
    setGeometry(150,150,800,280);

    initWidget();

    initConnect();
}

QEnrolDialog::~QEnrolDialog(){

}

void QEnrolDialog::initWidget(){

    mNameLabel = new QLabel(this);
    mNameLabel->setGeometry(10,10,70,20);
    mNameLabel->setText("FileName : ");

    mName = new QLineEdit(this);
    mName->setGeometry(80,10,500,20);

    mSizeLabel = new QLabel(this);
    mSizeLabel->setGeometry(590,10,70,20);
    mSizeLabel->setText("FileSize : ");

    mSize = new QLabel(this);
    mSize->setGeometry(670,10,120,20);
    mSize->setText("20000000");

    mHashLabel = new QLabel(this);
    mHashLabel->setGeometry(10,35,70,20);
    mHashLabel->setText("HashKey");

    mHash = new QLabel(this);
    mHash->setGeometry(90,35,700,20);
    mHash->setText("hash");

    mContentsLabel = new QLabel(this);
    mContentsLabel->setGeometry(10,60,100,20);
    mContentsLabel->setText("Contents");

    mContents = new QTextEdit(this);
    mContents->setGeometry(10,80,780,100);

    mCategoryLabel = new QLabel(this);
    mCategoryLabel->setGeometry(10,190,50,20);
    mCategoryLabel->setText("Category : ");

    mCategory = new QComboBox(this);
    mCategory->setGeometry(70,190,100,20);
    QSqlQuery query;
    query.exec("select name from category");
    while(query.next()){
        QString categoryName = query.value(0).toString();
        mCategory->addItem(categoryName);
    }

    mTagLabel = new QLabel(this);
    mTagLabel->setGeometry(180,190,50,20);
    mTagLabel->setText("   Tag : ");

    mTag0 = new QLineEdit(this);
    mTag1 = new QLineEdit(this);
    mTag2 = new QLineEdit(this);
    mTag3 = new QLineEdit(this);
    mTag4 = new QLineEdit(this);
    mTag0->setGeometry(240,190,100,20);
    mTag1->setGeometry(345,190,100,20);
    mTag2->setGeometry(450,190,100,20);
    mTag3->setGeometry(555,190,100,20);
    mTag4->setGeometry(660,190,100,20);

    mScoreLabel = new QLabel(this);
    mScoreLabel->setGeometry(10,220,50,20);
    mScoreLabel->setText("Score");

    mScore = new QSpinBox(this);
    mScore->setGeometry(70,220,50,20);

    mSaveButton = new QPushButton(this);
    mSaveButton->setGeometry(580,220,100,50);
    mSaveButton->setText("Save");

    mCancelButton = new QPushButton(this);
    mCancelButton->setGeometry(690,220,100,50);
    mCancelButton->setText("Cancel");

}

void QEnrolDialog::setFileInfo(QString str,QFileInfo *fileInfo){
    mName->setText(fileInfo->fileName());
    mSize->setText(QString("%1").arg(fileInfo->size(),0,10));
    mHash->setText(str);
}

void QEnrolDialog::initConnect(){

    connect(mCancelButton,SIGNAL(clicked()),this,SLOT(close()));
    connect(mSaveButton,SIGNAL(clicked()),this,SLOT(saveVideoSlot()));

}


void QEnrolDialog::saveVideoSlot(){

    QSqlQuery query;
    query.prepare("insert into video (key,size,name,category,contents,tag0,tag1,tag2,tag3,tag4,score) values (?,?,?,?,?,?,?,?,?,?,?) ");
    query.addBindValue(mHash->text());//key
    query.addBindValue(mSize->text().toInt());//size
    query.addBindValue(mName->text());//name
    query.addBindValue(mCategory->currentIndex()+1);//category
    query.addBindValue(mContents->toPlainText());//contents
    query.addBindValue(mTag0->text());//tag0
    query.addBindValue(mTag1->text());//tag1
    query.addBindValue(mTag2->text());//tag2
    query.addBindValue(mTag3->text());//tag3
    query.addBindValue(mTag4->text());//tag4
    query.addBindValue(mScore->text().toInt());//score
    query.exec();


    this->close();
    this->destroyed();

}


